<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增或编辑数据字典</title>
    <link th:replace="template/sys_header :: header"/>
</head>
<body>
<form lay-filter="form1" class="layui-form margintop20" action="">
    <input type="hidden" id="id" name="id" value=""/>
    <input type="hidden" id="parentId" name="parentId" value=""/>
    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>字典名称
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="name" name="name" lay-verify="required" autocomplete="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux" style="margin-left: 5px;">
            <span class="x-red">*</span>请输入数据字典名称
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            父级字典
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="parentName" autocomplete="" disabled="disabled" class="layui-input disabled">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            type
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="type" name="type"  autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            value
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="value" name="value" autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            排序号
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="sort" name="sort" lay-verify="number" autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            备注
        </div>
        <div class="layui-col-sm5">
            <textarea id="remark" name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">

        </div>
        <div class="layui-col-sm5" style="text-align: center">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save"><i class="iconfont">&#xe747;</i>
                &nbsp;保存&nbsp;
            </button>
            &nbsp;&nbsp;
            <button class="layui-btn layui-btn-danger" onclick="xadmin.close()"><i class="iconfont">&#xe747;</i>
                &nbsp;关闭&nbsp;
            </button>
        </div>
    </div>
</form>
<script type="text/javascript" th:replace="template/sys_header :: footer"></script>
<script type="text/javascript" th:inline="javascript">
    var table = layui.table, $ = layui.jquery, form = layui.form;
    var parentId = queryString("parentId");
    var parentName = decodeURIComponent(queryString("parentName"));
    var type = queryString("type");
    var dict = [[${dict}]];//组织机构信息
    var isEdit = false;//默认不是编辑模式
    console.log(dict);
    getById("parentName").value = parentName;
    getById("parentId").value = parentId;
    //如果是根目录则可以填写type
    if(type != ""){
        $("#type").val(type).addClass("disabled").attr("disabled","disabled");
    }

    if(dict && dict.id){
        isEdit = true;//编辑
        form.val('form1', {
            "id" : dict.id,
            "parentId": dict.parentId,
            "name": dict.name,
            "type": dict.type,
            "value": dict.value,
            "sort": dict.sort,
            "remark": dict.remark
        });
        //$("#remark").text(dict.remark);
    }

    $(function () {
        form.on('submit(save)', function (data) {
            console.log(data.field);
            //发异步，数据提交
            if(isEdit){
                edit(data.field);
            }else{
                add(data.field);
            }
            return false;
        });
    })

    function add(field) {
        console.log("add...");
        new AjaxRequest({
            type: "POST",
            url: webroot + "sys/dict/add",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        var id = res.resultObj;
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist(id , field.name , true);
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }

    function edit(field) {
        new AjaxRequest({
            type: "POST",
            url: webroot + "sys/dict/edit",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist(field.id , field.name , false);
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }
</script>
</body>
</html>